package com.xuecheng.search.domain.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.search.domain.service.IndexService;
import com.xuecheng.search.infrastructure.enums.EsResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-09-01  09:33
 */
@Slf4j
@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public Boolean addIndex(String indexName, String id, Object object) {
        String dataStr = JSON.toJSONString(object);
        IndexRequest indexRequest = new IndexRequest(indexName).id(id);
        indexRequest.source(dataStr, XContentType.JSON);
        IndexResponse indexResponse;
        try {
            indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        } catch (Exception e){
            log.error("es添加文档执行异常，{}", e.getMessage());
            throw new XueChengPlusException("es执行异常");
        }
        String result = indexResponse.getResult().name();
        return EsResultEnum.CREATED.getDesc().equalsIgnoreCase(result)
                || EsResultEnum.UPDATED.getDesc().equalsIgnoreCase(result);
    }

    @Override
    public Boolean deleteCourseIndex(String indexName,String id) {
        DeleteRequest deleteRequest = new DeleteRequest(indexName, id);
        DeleteResponse deleteResponse;
        try {
            deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        } catch (Exception e){
            log.error("es删除文档执行异常，{}", e.getMessage());
            throw new XueChengPlusException("es执行异常");
        }
        DocWriteResponse.Result result = deleteResponse.getResult();
        return EsResultEnum.DELETED.getDesc().equalsIgnoreCase(result.name());
    }

    @Override
    public Boolean updateCourseIndex(String indexName,String id, Object object) {
        UpdateRequest updateRequest = new UpdateRequest(indexName, id);
        updateRequest.doc(JSON.toJSONString(object), XContentType.JSON);
        UpdateResponse updateResponse;
        try {
            updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        } catch (Exception e){
            log.error("es更新文档执行异常，{}", e.getMessage());
            throw new XueChengPlusException("es执行异常");
        }
        DocWriteResponse.Result result = updateResponse.getResult();
        return EsResultEnum.UPDATED.getDesc().equalsIgnoreCase(result.name());
    }
}
